home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 342 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. Path: solon.com!not-for-mail
  2. From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 21 Feb 1996 10:15:52 -0600
  6. Organization: Los Alamos National Laboratory
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4gfgfp$3k8@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com> <4fvgvb$e0t@solutions.solon.com>
  11.     <4g55fl$avl@solutions.solon.com> <4gf99c$1fp@solutions.solon.com>
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. In article <4gf99c$1fp@solutions.solon.com> sl14@crux3.cit.cornell.edu
  15. (S. Lee) writes: 
  16. <snip>
  17.    In article <4g55fl$avl@solutions.solon.com>,  <msb@sq.com> wrote:
  18.    >> > short test(short x1, short x2) {
  19.    >> >  short result;
  20.    >> >  result = x1 + x2;
  21.    >> 
  22.    >> The addition operator "+" converts your short operands to ints.
  23.    >
  24.    >No, just using their values does that.  The operator + never sees
  25.    >operands narrower than int.  In the statement
  26.    >    x1; 
  27.    >the short is converted to int before being discarded.
  28.  
  29.    Okay, I'm confused.  Let just say the above is true.  Then the expression
  30.    "x1 + x2" has type int because x1 and x2 were converted to int before
  31.    being passed to the + operator.
  32.  
  33.    So what if sizeof(short) == sizeof(int)?  Assuming both x1 and x2 are
  34.    non-negative, would "x1 + x2" be unsigned int if it wouldn't fit in an
  35.    int?  In short, would there be undefined/implementation defined behaviour
  36.    if "result" is unsigned int?  How about long?
  37.  
  38. Why would x1+x2 become an unsigned int? It is x1 and x2 that are
  39. converted to ints: integral promotions occur on the operands, not on
  40. the result! When you add int's you get an int: and if the result does
  41. not fit, you are in trouble!
  42.  
  43. Furthermore, what x1 is promoted to does not depend on the _value_ of
  44. x1, but the _type_ of x1. If all values that the type can hold can fit
  45. into an int, it becomes an int: otherwise it becomes an unsigned
  46. int. Thus if x is unsigned short with value 2, USHRT_MAX is greater than
  47. INT_MAX, x is promoted to unsigned int and not to an int, because
  48. there exists some unsigned short value which does not fit into an int,
  49. even though the particular value 2 does. This is a general feature of
  50. C: the type of an expression does not depend on any _evaluation_
  51. (except for evaluation of integral constants, possible at compile
  52. time, to find array dimensions and the like).
  53.  
  54. The case you are talking of is even simpler. int must be able to hold
  55. all values that a short holds. So, if x1 and x2 are short, they are
  56. promoted to int, and x1+x2 is therefore also an int: and if the result
  57. does not fit, the compiler won't do anything to save you.
  58.  
  59. Cheers
  60. Tanmoy
  61. --
  62. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  63. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  64. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  65. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  66. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  67. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  68.